package com.ruoyi.web.controller.initData;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dao.SysUserDao;
import com.ruoyi.system.mapper.OaMapper;
import com.ruoyi.system.mapper.SysUserDaoMapper;
import com.ruoyi.tools.utils.pinyin.Pinyin4jUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


@Component
public class UserInit {

    @Autowired
    private OaMapper oaMapper;
    @Autowired
    private SysUserDaoMapper sysUserDaoMapper;

    /**
     * 初始化 用户
     */
    public void initUser() {
        List<Map<String, String>> list = oaMapper.listData("sys_t_user");
        if (list != null && list.size() > 0) {
            list.stream().forEach(item -> {
                if ("0".equals(String.valueOf(item.get("is_valid")))) {
                    return;
                }
                String empId = item.get("emp_id");
                List<Map<String, String>> valList = oaMapper.listDataByCondition("ent_t_humen_root", "emp_id", empId);
                Map<String, String> val = valList.get(0);

                SysUserDao user = new SysUserDao();
                user.setDeptId(null);
                user.setUserName(item.get("user_name"));
                user.setNickName(val.get("humen_name"));
                user.setUserType("00");
                user.setEmail(null);
                user.setPhonenumber(val.get("tel"));
                if (val.get("sex") != null) {
                    user.setSex("2");
                    if ("1".equals(String.valueOf(val.get("sex")))) {
                        user.setSex("0");
                    } else if ("2".equals(String.valueOf(val.get("sex")))) {
                        user.setSex("1");
                    }

                }
                user.setAvatar(null);
                user.setPassword(SecurityUtils.encryptPassword("123456"));
                user.setStatus("0");
                user.setDelFlag("0");
                user.setZhFullSpell(Pinyin4jUtil.toPinyin(val.get("humen_name"), false));
                user.setOldData(String.format("userUuid=%s,empId=%s", item.get("sys_t_user_uuid"), empId));
                sysUserDaoMapper.insert(user);
            });
        }
    }
}
